Module.c("plugin.uploadDialog");

$(document).ready(function(){plugin.uploadDialog.init();});

var plugin_uploadDialog_hasInit = false;
var uploadExtendData;


plugin.uploadDialog.init = function(){
	plugin.uploadDialog.initData();
	plugin.uploadDialog.renderPage();
	plugin.uploadDialog.bindEvent();
}


plugin.uploadDialog.initData = function(){
	plugin_uploadDialog_hasInit = false;
}

plugin.uploadDialog.renderPage=function(){
	
	$('#uploadFileBt').button();
	$('#plugin_uploadDialog').dialog({ 
		autoOpen: false,
		modal   : true,
		width   : 380,
		title   : '文件上传',
		resizable:false
	});
	
}

plugin.uploadDialog.bindEvent=function(){
	$('#uploadFileBt').click(plugin.uploadDialog.uploadFile);
}



plugin.uploadDialog.initComponent = function(option,extData){
	
	console.debug('option values for init uploadFile plugin is:');
	console.debug(option);
	console.debug('extData values for init uploadFile plugin is:');
	console.debug(extData);
	
	uploadExtendData = extData;
	if (plugin_uploadDialog_hasInit) return;
	
	//set Default Value;
	var queueSizeLimit=999;
	var fileDesc = '所有文件(*.*)';
	var fileExt = '*.*';
	var folder = '/';
	
	
	//set value via option user define
	var scriptUri = option['scriptUri']+';jsessionid='+sessionId;
	if (option['queueSizeLimit']!=null){
		queueSizeLimit = option['queueSizeLimit'];
	}
	if (option['fileDesc']!=null){
		fileDesc = option['fileDesc'];
	}
	if (option['fileExt']!=null){
		fileExt = option['fileExt'];
	}
	if (option['folder']!=null){
		folder = option['folder'];
	}
	
	
	$('#file_upload').uploadify({
		    'uploader'  : 'javascript/plugin/uploadify/uploadify.swf',
		    'script'    : scriptUri,
		    'queueID'        : 'plugin_uploadForm_fileQueue',
		    'queueSizeLimit' : queueSizeLimit,
		    'fileDesc'    	 : fileDesc ,
		    'fileExt'     	 : fileExt,
		    'auto'           : false,
        	'multi'          : true,
        	'simUploadLimit' : 1,
        	'buttonText'     : 'Add File',
		    'cancelImg' 	: 'javascript/plugin/uploadify/cancel.png',
		    'folder'    	: folder,
		    'wmode' 		 : 'transparent',
        	'removeCompleted': false,
        	'displayData' 	 : 'speed',
		    'method'         : 'Get' ,
		    'onSelectOnce'   : plugin.uploadDialog.refreshTitleInfo,
        	'onCancel'   	 : plugin.uploadDialog.refreshTitleInfo,
        	'onComplete'      : plugin.uploadDialog.onCompelete
	});
	
	
	plugin_uploadDialog_hasInit = true;
	
}


plugin.uploadDialog.refreshTitleInfo = function(event,data){
	
	
	fileCount = data.fileCount;
	fileBytes = data.allBytesTotal;
	
	
	
	
	if (fileCount==0||fileCount==null){
		$('#plugin_uploadDialog').dialog({ 
			title   : '文件上传'
		});
	}else{
		$('#plugin_uploadDialog').dialog({ 
			title   : '文件上传('+fileCount+'个文件,共'+util.fileSizeDisplay(fileBytes)+')'
		});
	}
	
}

plugin.uploadDialog.uploadFile = function(){
	
	$('#file_upload').uploadifySettings('scriptData',uploadExtendData);
	$('#file_upload').uploadifyUpload();
}


plugin.uploadDialog.onCompelete =function(event, ID, fileObj, response, data) {
		var responseVO = eval('('+response+')');
		$('#plugin_uploadDialog').trigger(_EVENT_ONE_FILE_UPLOAD_COMPLETE,responseVO);
}
